import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;

public class leetcode853_carline {
    public static void main(String[] args) {
        int target = 12;
        int[] position = new int[]{10,8,0,5,3};
        int[] speed = new int[]{2,4,1,1,3};
        System.out.println(carFleet(target, position, speed));
    }
    public static int carFleet(int target, int[] position, int[] speed) {
//        int len = speed.length;
        HashSet<Double> set = new HashSet<>();
        car[] cars = new car[speed.length];
        for (int i = 0; i < speed.length; i++) {
            cars[i] = new car(position[i], (target - position[i]) / (double)speed[i]);
        }

        Arrays.sort(cars,(a1, a2) ->{
            return Integer.compare(a2.pos,a1.pos);
        });
        for (car car : cars) {
            System.out.println(car);
        }

        int i = 0;
        int j = 1;

        while (j < speed.length) {
            while (cars[j].ti <= cars[i].ti) {
                j++;

                if(j >= speed.length){
                    break;
                }
            }
            while (i < j-1) {
                cars[i+1].ti = cars[i].ti;
                i++;
            }
            i = j;
            j++;

        }

        for (int cnt = 0; cnt < speed.length; cnt++){
            set.add(cars[cnt].ti);
        }
        return set.size();

    }
}

class car {
    public int pos;
    public double ti;

    public car(int pos, double ti) {
        this.pos = pos;
        this.ti = ti;
    }

    @Override
    public String toString() {
        return "car{  "+pos+"  ," + ti + '}';
    }


}
